package com.goods.controller.business;

import com.goods.business.service.SupplierService;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "业务管理-物资来源相关接口")
@RestController
@RequestMapping("/business/supplier")
public class SupplierController {


    @Autowired
    private SupplierService supplierService;

    //物资来源列表显示
    @GetMapping("/findSupplierList")
    public ResponseBean<PageVO<SupplierVO>> findSupplierList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize,
            SupplierVO supplierVO){

      PageVO<SupplierVO>  supplierVOPageVO= supplierService.findSupplierList(pageNum,pageSize,supplierVO);
        return ResponseBean.success(supplierVOPageVO);
    }

    //添加物资来源
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated SupplierVO supplierVO) throws BusinessException {
        supplierService.add(supplierVO);
        return ResponseBean.success();
    }

    //删除物资来源
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) throws BusinessException{
        supplierService.delete(id);
        return ResponseBean.success();

    }

    //修改之前回显物资来源
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
       Supplier supplier= supplierService.edit(id);
        return ResponseBean.success(supplier);
    }

    //修改物资来源
    @PutMapping("/update/{id}")
    public ResponseBean update(
            @PathVariable Long id,
            @RequestBody @Validated Supplier supplierVO) throws BusinessException{
        supplierService.updateSupplier(id,supplierVO);
        return ResponseBean.success();

    }

    //添加入库单（物资入库） 物资来源
    @GetMapping("/findAll")
    public ResponseBean<List<Supplier>> findAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                SupplierVO supplierVO){
        List<Supplier> list = supplierService.findSupplierList1(supplierVO.getName(),supplierVO.getContact(),supplierVO.getContact());
        List<Supplier> page = ListPageUtils.page(list, pageSize, pageNum);
        PageVO pageVO = new PageVO(page.size(), page);
        return ResponseBean.success(list);
    }


}
